package com.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.app.util.cHighTimer;

import lib.common.R;


public class SurfaceViewCircle extends SurfaceView implements SurfaceHolder.Callback2, Runnable
{

	private static String TAG = SurfaceViewCircle.class.getSimpleName();

	private Context mContext;
	private SurfaceHolder mHolder;

	private Paint mPaintCircle; //内圆paint
	private int mColorCircle = Color.parseColor("#eeeeee");
	private Point mPointCenter;  //圆心
	private int mRadius ; //内圆半径
	
	private Paint mPaintRing;	//外圆环paint
	private int mColorRing = Color.parseColor("#111111");
	private int mRingSize ;
	private RectF mRectRing = new RectF();
	private int mRadiusRing = mRadius;  //外环半径
	private int alpha = 0xff; //外环透明度
	private int mBackgroundColor = Color.GRAY; // 背景色

	private cHighTimer mHighTimer = new cHighTimer();
	private boolean bRun = false;

	int mRectRingScale = 3;


	/*
	public CircleSurfaceView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		

		init(context, null, 0);
		
	}
	*/
	
	///*
	public SurfaceViewCircle(Context context, AttributeSet set){
		super(context, set);
		
		init(context, set, 0);
		
	}
	//*/


	private void init(Context context, AttributeSet attrs, int defStyle) {
		// Load attributes


		mContext = context;


		mHolder = this.getHolder();
		mHolder.addCallback(this);


		if (null != attrs) {
			TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyCircleSurfaceView);

			if (ta != null) {

				mRadius = ta.getDimensionPixelSize(R.styleable.MyCircleSurfaceView_MyCircleSurfaceViewRadiusSmall, 50);
				mRadiusRing = ta.getDimensionPixelSize(R.styleable.MyCircleSurfaceView_MyCircleSurfaceViewRadiusBig, mRadius*mRectRingScale);
				mRectRingScale = ta.getDimensionPixelSize(R.styleable.MyCircleSurfaceView_MyCircleSurfaceViewRadiusBigExtendsStorkeSize, 3);
				mRingSize = ta.getDimensionPixelSize(R.styleable.MyCircleSurfaceView_MyCircleSurfaceViewRadiusBigExtendsStorkeSize, 5);

				mColorCircle = ta.getColor(R.styleable.MyCircleSurfaceView_MyCircleSurfaceViewColorRadiusSmall, mColorCircle);
				mColorRing = ta.getColor(R.styleable.MyCircleSurfaceView_MyCircleSurfaceViewColorRadiusBig, mColorRing);
				// ColorGroupCircle = ta.getColor(R.styleable.timeline_ColorRadiusBig, getResources().getColor(android.R.color.holo_green_light));


				ta.recycle();
			}
		} else {


		}


	}
	
	

	/*
	public void SetPointCenter(Point center){
		mPointCenter = center;

	}

	public void SetBackground(int BackgroundColor){
		mBackgroundColor = BackgroundColor;
	}
	*/

	
	void initCircle(int x, int y){

		// 设置圆心
		if(mPointCenter == null){
			mPointCenter = new Point(x,y);
		}

		// 设置内圆的颜色
		mPaintCircle = new Paint();
		mPaintCircle.setColor( mColorCircle );

		// 设置扩散圆环的颜色
		mPaintRing = new Paint();
		mPaintRing.setColor( mColorRing );
		mPaintRing.setStyle(Style.STROKE);
		mPaintRing.setStrokeWidth( mRingSize );
		
		//启动计时器
		mHighTimer.start_counter();
	 
	}
	

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub

		int width = getWidth();
		int height = getHeight();

		initCircle(width / 2, height / 2);

		bRun = true;
		new Thread(this).start();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		bRun = false;
	}

	@Override
	public void surfaceRedrawNeeded(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		while(bRun){
			float elapsed = mHighTimer.get_elapsed_time();
			if( mHighTimer.controlFPS_Return(elapsed, 33) ){
				synchronized (mHolder) {
					Update();
					Draw();
				}	
			}
			
		}
		
		
	}	
	
	
	
	void Update(){

		// 外环最大扩展是50的3倍；不断扩展不断淡化
		if(mRadiusRing > mRadius*mRectRingScale || alpha < 0x10){
			mRadiusRing = mRadius;
			alpha = 0xff;
		}
		else{
			mRadiusRing+=1;
			alpha -=5;
		}


		///*
		// 外环的包围框，用于调试使用
    	mRectRing.left =mPointCenter.x-mRadiusRing;
    	mRectRing.top =mPointCenter.y-mRadiusRing;
    	mRectRing.right =mPointCenter.x+mRadiusRing;
    	mRectRing.bottom =mPointCenter.y+mRadiusRing;
    	//Utils_Log.d(TAG, "mRectRing: "+mRectRing.toString() );
    	//*/

		mPaintRing.setAlpha(alpha);
	}
	
	
	
	void Draw(){
		

		Canvas canvas = mHolder.lockCanvas();
		if(canvas == null)
			return;

		// 绘制背景颜色
		canvas.drawColor(mBackgroundColor); 
		
    	// 绘制中心
		canvas.drawCircle(mPointCenter.x, mPointCenter.y, mRadius, mPaintCircle);

		// 绘制扩散的环
		canvas.drawOval(mRectRing, mPaintRing);
    	
		
		mHolder.unlockCanvasAndPost(canvas);
		
		
	}







};

